package com.yami.shop.admin.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.CheckAccount;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.CheckAccountService;
import com.yami.shop.service.ShopDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 财务对账
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {
    @Autowired
    private CheckAccountService checkAccountService;
    @Autowired
    private ShopDetailService shopDetailService;
    /**
     * 获取后台首页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<CheckAccount>> page(CheckAccount checkAccount, PageParam<CheckAccount> page){
        Long shopid = SecurityUtils.getSysUser().getShopId();
        checkAccount.setShopid(shopid);
        IPage<CheckAccount> shopDetails = null;
        if(null!=shopid) {
            if(shopid.equals(1)||shopid==1l){
                shopDetails = checkAccountService.pageCheckAccountByOrderParam(page,checkAccount);
            }else{
                shopDetails = checkAccountService.pageCheckAccountByOrderParam(page,checkAccount);
            }

        }
        return ResponseEntity.ok(shopDetails);
    }

    @GetMapping("/getAccount")
    public ResponseEntity<Map>  getAccount(){
        Map map = new HashMap();
        Double account=0.00;
        ShopDetail shopDetail;
        Long shopid = SecurityUtils.getSysUser().getShopId();
        if(shopid!=null) {
             shopDetail = shopDetailService.getShopDetailByShopId(shopid);
             if(shopDetail!=null){
                  account = shopDetail.getCashMoney();
             }
        }
        map.put("account",account);
        //获取所有订单的返现金额
        Double rebateMoneyAmount = checkAccountService.getRebateMoneyAmount();
        map.put("rebateMoneyAmount",rebateMoneyAmount);
        return  ResponseEntity.ok(map);
    }



}
